package com.pilipili.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.pilipili.commonutils.Result;
import com.pilipili.servicebase.handler.MyException;
import com.pilipili.vod.service.VodService;
import com.pilipili.vod.utils.AliyunVodSDKUtils;
import com.pilipili.vod.utils.VodUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author Unlimited_Blade_Works
 */

@RestController
@RequestMapping("/vod/video")
public class VodController {
    @Autowired
    private VodService vodService;
    @ApiOperation(value = "上传视频")
    @PostMapping("upload")
    public Result uploadVideo(@ApiParam(name = "file", value = "文件", required = true)
                                @RequestParam("file") MultipartFile file) {

        String videoId = vodService.uploadVideo(file);
        return Result.ok().message("视频上传成功").data("videoId", videoId);
    }@ApiOperation(value = "删除所有视频")
    @DeleteMapping("deleteVideos")
    public Result deleteVideos(@RequestParam List<String> videoIdList) {
        vodService.deleteVideos(videoIdList);
            return Result.ok();
    }
    @ApiOperation(value = "删除视频")
    @DeleteMapping("deleteVideo/{id}")
    Result deleteVideo(@PathVariable("id") String id){
        vodService.deleteVideo(id);
        return Result.ok();
    }
    @ApiOperation(value = "阿里云获取播放视频地址")
    @GetMapping("videoPlayer/{videoId}")
    public Result getVideoPlayAuth(@PathVariable("videoId") String videoId) {

        try {
            //初始化
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(VodUtil.ACCESS_KEY_ID, VodUtil.ACCESS_KEY_SECRET);
            //请求
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(videoId);
            //响应
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            //得到播放凭证
            String playAuth = response.getPlayAuth();
            //返回结果
            System.out.println(playAuth);
            return Result.ok().message("获取凭证成功").data("playAuth", playAuth);
        } catch (Exception e) {
            throw new MyException(20001,"获取凭证失败");
        }
    }
}
